package view;

import java.util.List;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.bean.Location;
import model.dao.LocationDAO;

public class PnlUserLocationList extends JPanel {
	
	// Variables declaration - do not modify//GEN-BEGIN:variables
    private JScrollPane spnlLocationData;
    private JScrollPane spnlLocationData1;
    private JTable tblData;
    // End of variables declaration//GEN-END:variables
    DefaultTableModel tblModel = new DefaultTableModel();

    /** create form LocationEmployeeUIPanel */
    public PnlUserLocationList() {
        initComponents();
     
        loadTableData();
    }

    private void initComponents() {

        spnlLocationData = new JScrollPane();
        spnlLocationData1 = new JScrollPane();
        tblData = new JTable();

        tblData.setModel(new DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        spnlLocationData1.setViewportView(tblData);

        spnlLocationData.setViewportView(spnlLocationData1);

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(spnlLocationData, GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(spnlLocationData, GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loadTableData() {
        //try {
            List<Location> locationList = LocationDAO.selectAllLocation();
            tblModel.addColumn("Location id");
            tblModel.addColumn("Location name");
            tblModel.addColumn("Number employees");
           for (Location location : locationList) {
                Vector<String> row = new Vector<String>();
                row.addElement(String.valueOf(location.getLocationId()));
                row.addElement(location.getLocationName());
                row.addElement(String.valueOf(location.getNumberOfEmployees()));
                tblModel.addRow(row);
            }
            tblData.setModel(tblModel);
//        } catch (ClassNotFoundException ex) {
//            JOptionPane.showMessageDialog(null,
//                   "",
//                    "",
//                    JOptionPane.ERROR_MESSAGE);
//            ex.printStackTrace();
//        } catch (SQLException ex) {
//            JOptionPane.showMessageDialog(null,
//                    "",
//                    "",
//                    JOptionPane.ERROR_MESSAGE);
//            ex.printStackTrace();
//        }
    }

   
    
}
